package com.silentfish.backend.codeGenerator;

import com.silentfish.core.baseModel.Pagination;
import com.silentfish.backend.codeGenerator.bean.Conf;
import com.silentfish.backend.codeGenerator.bean.GenFileInfo;
import com.silentfish.backend.codeGenerator.bean.TableInfo;
import com.silentfish.backend.codeGenerator.creator.FileCreator;
import com.silentfish.backend.codeGenerator.factory.SimpleFactory;
import com.silentfish.backend.codeGenerator.mapper.CodeGeneratorMapper;
import com.silentfish.backend.codeGenerator.util.DbUtils;
import freemarker.template.TemplateException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/codeGen")
public class CodeGeneratorController {

//    @Resource
//    private CodeGeneratorMapper codeGeneratorMapper;

    @RequestMapping("/toPage")
    public String toPage() {
        return "codeGen/codeGen";
    }

    @RequestMapping("/toPage1")
    public String toPage1() {
        return "erd/erd";
    }

    @RequestMapping("/toPage2")
    public String toPage2() {
        return "erd/index";
    }

//    @RequestMapping("/getTableList")
//    public @ResponseBody Pagination<TableInfo> getTableList(CodeGeneratorDTO queryDto) {
//        Pagination<TableInfo> page = new Pagination<>();
//        page.setData(codeGeneratorMapper.getTableList(queryDto));
//        page.setRecordsTotal(222);
//        page.setRecordsFiltered(222);
//        return page;
//    }
//
//    @RequestMapping("/getCode")
//    public @ResponseBody void getCode(HttpServletResponse response,String tableName)throws SQLException, ClassNotFoundException, IOException, TemplateException {
//        // 基础信息
//        Conf conf = new Conf().getConf();conf.setTables(tableName);
//        // 表集合
//        List<TableInfo> tableInfos = DbUtils.getInstance().getAllTables(conf);
//
//        // 生成文件
//        List<GenFileInfo> genFileInfoList = this.createFile(conf, tableInfos);
//
//        response.setContentType("application/octet-stream");
//        response.setHeader("Content-Disposition", "attachment;filename=code.zip");
//
//        ZipOutputStream zo = new ZipOutputStream(response.getOutputStream());
////        zo.setEncoding("UTF-8");
//        for (GenFileInfo genFileInfo : genFileInfoList) {
//
////            System.out.print(new String(genFileInfo.getOs().toByteArray(), StandardCharsets.UTF_8));
//            try (ByteArrayInputStream is = new ByteArrayInputStream(genFileInfo.getOs().toByteArray())) {
//                zo.putNextEntry(new ZipEntry(genFileInfo.getFileName()));
//                int len;
//                byte[] buffer = new byte[1024];
//                while ((len = is.read(buffer, 0, buffer.length)) > 0) {
//                    zo.write(buffer, 0, len);
//                }
//                zo.flush();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            zo.closeEntry();
//
//        }
//        zo.close();
//
//    }
//
//    private List<GenFileInfo> createFile(Conf conf, List<TableInfo> tableInfos)  throws IOException, TemplateException{
//
//        List<GenFileInfo> genFileInfoList = new ArrayList<>();
//
//        //要生成的文件
//        List<String> modules = conf.getModules();
//
//        FileCreator creator;
//        for (TableInfo tableInfo : tableInfos) {
//            for (String module : modules) {
//                creator = SimpleFactory.create(module, conf, tableInfo);
//                genFileInfoList.add(creator.createFile(tableInfo));
//            }
//        }
//
//        return genFileInfoList;
//    }

}
